

import Router from '@koa/router';
import db from '../mysqlHelper.js';
import logger from '../logger.js';
import response from '../response.js';
import moment from 'moment';
import md5 from 'md5';
import axios from 'axios';
import redis from '../redis.js';

import meter from '../meter.js'


const router = new Router();
router.prefix('/api');

//拉合闸控制
router.post('/oprtation/switchControl', async (ctx) => {
    let body = ctx.request.body;
    if (ctx.state.oper?.name == null) {
        ctx.body = response.fail('非法的操作员禁止调用该接口');
        return;
    }
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if (body.mac == null) {
        ctx.body = response.fail('缺少必填字段 电表mac');
        return;
    }
    if (body.status == null) {
        ctx.body = response.fail('缺少必填字段 开关状态');
        return;
    }
    if (body.model == null) {
        ctx.body = response.fail('缺少必填字段 设备型号');
        return;
    }
    console.log(`操作员:${ctx.state.oper.name} 调用电表开关控制接口 mac:${ctx.request.body.mac} 开关状态:${ctx.request.body.status}`);
    if (body.model == 'DDSY2268') {
        let res = await meter.switchControl(body.mac, body.status);
        if (res.code == 1) {
            ctx.body = response.success();
            return;
        } else {
            ctx.body = response.fail(res.msg);
            return;
        }
    }
});
//读取实时状态
router.post('/oprtation/getRealData', async (ctx) => {
    let body = ctx.request.body;
    if (ctx.state.oper?.name == null) {
        ctx.body = response.fail('非法的操作员禁止调用该接口');
        return;
    }
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if (body.mac == null) {
        ctx.body = response.fail('缺少必填字段 电表mac');
        return;
    }
    if (body.model == null) {
        ctx.body = response.fail('缺少必填字段 设备型号');
        return;
    }
    if (body.model == 'DDSY2268') {
        let res = await meter.getEMT(body.mac);
        if (res.code == 1) {
            ctx.body = response.success(res.data);
            return;
        } else {
            ctx.body = response.fail(res.msg);
            return;
        }
    }
})
//读取电表参数
router.post('/oprtation/getMeterParam', async (ctx) => {
    let body = ctx.request.body;
    if (ctx.state.oper?.name == null) {
        ctx.body = response.fail('非法的操作员禁止调用该接口');
        return;
    }
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if (body.mac == null) {
        ctx.body = response.fail('缺少必填字段 电表mac');
        return;
    }
    if (body.param_no == null) {
        ctx.body = response.fail('缺少必填字段 参数编号');
        return;
    }
    if (body.model == null) {
        ctx.body = response.fail('缺少必填字段 设备型号');
        return;
    }
    if (body.model == 'DDSY2268') {
        let res = await meter.queryParam(body.mac, body.param_no);
        if (res.code == 1) {
            ctx.body = response.success(res.data);
            return;
        } else {
            ctx.body = response.fail(res.msg);
            return;
        }
    }
})
//设置电表参数
router.post('/oprtation/setMeterParam', async (ctx) => {
    let body = ctx.request.body;
    if (ctx.state.oper?.name == null) {
        ctx.body = response.fail('非法的操作员禁止调用该接口');
        return;
    }
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if (body.mac == null) {
        ctx.body = response.fail('缺少必填字段 电表mac');
        return;
    }
    if (body.param_no == null) {
        ctx.body = response.fail('缺少必填字段 参数编号');
        return;
    }
    if (body.param_value == null) {
        ctx.body = response.fail('缺少必填字段 参数值');
        return;
    }
    if (body.model == null) {
        ctx.body = response.fail('缺少必填字段 设备型号');
        return;
    }
    if (body.model == 'DDSY2268') {
        let res = await meter.writeParam(body.mac, body.param_no, body.param_value);
        if (res.code == 1) {
            ctx.body = response.success();
            return;
        } else {
            ctx.body = response.fail(res.msg);
            return;
        }
    }
})
//重启电表
router.post('/oprtation/rebootMeter', async (ctx) => {
    let body = ctx.request.body;
    if (ctx.state.oper?.name == null) {
        ctx.body = response.fail('非法的操作员禁止调用该接口');
        return;
    }
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if (body.mac == null) {
        ctx.body = response.fail('缺少必填字段 电表mac');
        return;
    }
    if (body.model == null) {
        ctx.body = response.fail('缺少必填字段 设备型号');
        return;
    }
    if (body.model == 'DDSY2268') {
        let res = await meter.reboot(body.mac);
        if (res.code == 1) {
            ctx.body = response.success();
            return;
        } else {
            ctx.body = response.fail(res.msg);
            return;
        }
    }
});

router.post('/oprtation/setEME', async (ctx) => {

    if (ctx.state.oper?.name == null) {
        ctx.body = response.fail('非法的操作员禁止调用该接口');
        return;
    }

    let body = ctx.request.body;
    if (body == null) {
        ctx.body = response.fail('参数不允许为空');
        return;
    }
    if (body.mac == null) {
        ctx.body = response.fail('缺少必填字段 电表mac');
        return;
    }
    if (body.model == null) {
        ctx.body = response.fail('缺少必填字段 设备型号');
        return;
    }
    if (body.value == null) {
        ctx.body = response.fail('缺少必填字段 EME值');
        return;
    }
    console.log(`操作员:${ctx.state.oper.name} 调用设置电表EME接口 mac:${ctx.request.body.mac} EME值:${ctx.request.body.value}`);
    if (body.model == 'DDSY2268') {
        let res = await meter.setEME(body.mac, body.value);
        if (res.code == 1) {
            ctx.body = response.success();
            let hand_rec = {
                oper_time: moment().format('YYYY-MM-DD HH:mm:ss'),
                oper_name: ctx.state.oper.name,
                set_value: body.value,
                meter_mac: body.mac

            }
            let sql = `select name,eme from meter where mac='${body.mac}'`;
            let meter = await db.query(sql);
            if (meter instanceof Error) {
                console.warn(`查询电表信息出错 error :` + meter.message, `mac:${body.mac}`);
            }
            hand_rec.meter_name = meter[0].name;
            hand_rec.old_value = meter[0].eme;
            let res = await db.create('hand_set_eme_rec', hand_rec);
            if (res instanceof Error) {
                console.error(`插入手动操作记录失败: ${res.message}`);
            } 

            return;
        } else {
            ctx.body = response.fail(res.msg);
            return;
        }
    }
});

//开始下载收费规则到充电桩
















export { router };